<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="search" placeholder="用户" class="filter-item" style="width: 400px;" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      v-if="list"
      :key="tableKey"
      v-loading="listLoading"
      :data="list.slice((currentPage - 1) * pageSize,currentPage * pageSize)"
      border
      fit
      highlight-current-row
      style="width: 90%"
    >

      <el-table-column label="姓名" prop="name" align="center" width="300" sortable>
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="邮箱" prop="email" align="center" width="300">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>

      <el-table-column label="手机号" prop="phone" align="center" width="300" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :total="total"
      :page.sync="pageSize"
      :current.sync="currentPage"
      :page-sizes="pageSizes"
      @pagination="getList"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="70px" style="width:400px; margin-left:50px;">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="temp.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="temp.phone" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateData()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getAllUser, updateUser, deleteUser } from '@/api/alertm'

export default {
  name: 'LogTable',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      // dialog
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      temp: {
        name: '',
        email: '',
        phone: ''
      },
      // pagination
      total: 0,
      currentPage: 1,
      pageSize: 10,
      pageSizes: [10, 20, 30],
      // search
      search: '',
      // raw list data
      rawList: null,
      // table list data
      list: null,
      listLoading: true
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true

      // 后端返回数据格式, 因为邮箱是唯一
      // this.rawList = [
      //   {
      //     'name': 'huan.jiang2',
      //     'email': 'huan.jiang2@zhenai.com',
      //     'phone': '12345678901'
      //   },
      //   {
      //     'name': 'sisi.li8',
      //     'email': 'sisi.li8@zhenai.com',
      //     'phone': '12345678900'
      //   },
      //   {
      //     'name': 'Tom',
      //     'email': 'huan.jiang2@zhenai.com',
      //     'phone': '12345678900'
      //   },
      //   {
      //     'name': 'Sue',
      //     'email': 'huan.jiang2@zhenai.com',
      //     'phone': '12345678903'
      //   }
      // ]
      // this.list = this.rawList
      // this.total = this.list.length
      // this.listLoading = false

      getAllUser()
        .then(response => {
          this.rawList = response.data.data
          this.list = this.rawList
          this.total = this.list.length
        })
        .catch(error => { console.log(error) })
        .finally(() => { this.listLoading = false })
    },

    handleFilter() {
      if (this.search.length === 0) {
        this.list = this.rawList
      } else {
        this.list = this.rawList.filter(data => !this.search || data.name.toLowerCase().includes(this.search.toLowerCase()))
      }
      this.total = this.list.length
    },

    resetTemp() {
      this.temp = {
        name: '',
        email: '',
        phone: ''
      }
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          updateUser(this.temp.email, this.temp)
            .then(res => {
              if (res.data.code === 200) {
                this.$notify({
                  title: res.data.status,
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
            })
            .catch(error => { console.log(error) })
        }
      })
    },
    handleDelete(index, row) {
      this.temp = Object.assign({}, row)
      deleteUser(this.temp.email)
        .then(res => {
          if (res.data.code === 200) {
            this.$notify({
              title: res.data.status,
              type: 'success',
              duration: 2000
            })
            this.list.splice(index, 1)
          } else {
            this.$notify({
              title: res.data.error,
              type: 'error',
              duration: 2000
            })
          }
        })
        .catch(error => { console.log(error) })
    }
  }
}
</script>

<style scoped>
.pagination-container {
  background: #fff;
  padding: 32px 16px;
}
</style>
